<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" show-summary class="-mt-10px">
      <el-table-column label="序号" type="index" align="center" width="60" />
      <el-table-column label="样本名称" min-width="60">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.productId`" :rules="formRules.productId" class="mb-0px!">
            <el-select
              v-model="row.productId"
              clearable
              filterable
              @change="onChangeProduct($event, row)"
              placeholder="请选择样本"
            >
              <el-option
                v-for="item in productList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="样本价格" width="80">
        <template #default="{ row }">
          <el-form-item class="mb-0px!">
            {{ productUnitNameFun(row.productId) }}
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="终止日期" min-width="80">
        <template #default="{ row }">
          <el-date-picker
            v-model="row.productExpirationDate"
            type="date"
            value-format="x"
            placeholder="选择终止日期"
            class="!w-160px"
          />
        </template>
      </el-table-column>
      <el-table-column label="生效日期" min-width="80">
        <template #default="{ row }">
          <el-date-picker
            v-model="row.productEffectiveDate"
            type="date"
            value-format="x"
            placeholder="选择生效日期"
            class="!w-160px"
          />
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link type="primary">删除此条</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3">
    <el-button @click="handleAdd" round>+ 添加物料价格关联</el-button>
  </el-row>
</template>
<script setup lang="ts">
import { ProductApi, ProductVO } from '@/api/erp/product/product'
import { StockApi } from '@/api/erp/stock/stock'
import {
  erpCountInputFormatter,
  erpPriceInputFormatter,
  erpPriceMultiply,
  getSumValue
} from '@/utils'

const props = defineProps<{
  items: undefined
  disabled: false
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref([])
const formRules = reactive({
  productId: [{ required: true, message: '样本不能为空', trigger: 'blur' }],
  price: [{ required: true, message: '样本单价不能为空', trigger: 'blur' }],
  productExpirationDate: [{ required: true, message: '终止日期不能为空', trigger: 'blur' }],
  productEffectiveDate: [{ required: true, message: '生效日期不能为空', trigger: 'blur' }]
})
const formRef = ref([]) // 表单 Ref
const productList = ref<ProductVO[]>([]) // 样本列表

/** 初始化设置入库项 */
watch(
  () => props.items,
  async (val) => {
    formData.value = val
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    productId: undefined,
    price: undefined,
    productExpirationDate: undefined,
    productEffectiveDate: undefined
  }
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index: number) => {
  formData.value.splice(index, 1)
}

/** 处理样本变更 */
const onChangeProduct = (productId, row) => {
  const product = productList.value.find((item) => item.id === productId)
  if (product) {
    row.price = product.purchasePrice
  }
}
// 处理单位回显
const productUnitNameFun = (productId: any) => {
  const product = productList.value.find((item) => item.id === productId)
  return product ? product.purchasePrice : ''
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}
defineExpose({ validate })

/** 初始化 */
onMounted(async () => {
  productList.value = await ProductApi.getProductSimpleList()
  // 默认添加一个
  console.log(formData.value)

  if (formData.value.length == 0) {
    handleAdd()
  }
})
</script>
